package com.iris.egrant.dao.proposal;


import com.iris.egrant.exception.DaoException;
import com.iris.egrant.model.proposal.Proposal;
import com.iris.egrant.model.proposal.ProposalCached;
import com.iris.egrant.orm.hibernate.HibernateDao;
import org.apache.commons.lang.math.NumberUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Repository
public class ProposalDao
        extends HibernateDao<Proposal, Long> {
    @Autowired
    private ProposalCachedExtendDao proposalCachedExtendDao;

    public boolean involveDepartment(Long deptCode) {
        /*  60 */
        String sql = "select count(*) from proposal pro where pro.dept_code = ?";
        /*  61 */
        BigDecimal count = (BigDecimal) getSession().createSQLQuery(sql).setLong(0, deptCode.longValue()).uniqueResult();
        /*  62 */
        return (count.intValue() != 0);
    }


    public List<Proposal> findAllByCodes(List<Long> prpCodes) {
        /*  67 */
        String hql = "select t from Proposal t  where t.prpCode in ( :prpCodes )";
        /*  68 */
        Query query = createQuery(hql, new Object[0]);
        /*  69 */
        query.setParameterList("prpCodes", prpCodes);
        /*  70 */
        return query.list();
    }


    public boolean existInBpm2Dept(Long deptCode) {
        /*  81 */
        StringBuilder hql = new StringBuilder();
        /*  82 */
        hql.append("select count(*) from Proposal p where p.deptCode = ? and status = '").append("02").append("'");

        /*  84 */
        Long count = (Long) createQuery(hql.toString(), new Object[]{deptCode}).uniqueResult();
        /*  85 */
        return (count.longValue() != 0L);
    }


    public List<Proposal> findAllByDept(Long deptCode) {
        /*  96 */
        StringBuilder hql = new StringBuilder();
        /*  97 */
        hql.append("from Proposal p where p.deptCode = ?");
        /*  98 */
        return createQuery(hql.toString(), new Object[]{deptCode}).list();
    }


    public Proposal getProposalByPoscode(Long pos_code) {
        /* 109 */
        StringBuilder hql = new StringBuilder();
        /* 110 */
        hql.append("from Proposal p where p.posCode = ?");
        /* 111 */
        return (Proposal) createQuery(hql.toString(), new Object[]{pos_code}).uniqueResult();
    }

    public Proposal getProposalByPrpcode(Long prp_code) {
        /* 115 */
        StringBuilder hql = new StringBuilder();
        /* 116 */
        hql.append("from Proposal p where p.prpCode = ?");
        /* 117 */
        return (Proposal) createQuery(hql.toString(), new Object[]{prp_code}).uniqueResult();
    }

    public List<Proposal> getProposalByGrantcode(Long grant_code) {
        /* 121 */
        StringBuilder hql = new StringBuilder();
        /* 122 */
        hql.append("from Proposal p where p.grantCode = ?");
        /* 123 */
        return createQuery(hql.toString(), new Object[]{grant_code}).list();
    }


    public void updateDeptOfProposal(Long oldDeptCode, Long newDeptCode) {
        /* 134 */
        StringBuilder hql = new StringBuilder();
        /* 135 */
        hql.append("update Proposal p set p.deptCode = ? where p.deptCode = ?");
        /* 136 */
        getSession().createQuery(hql.toString()).setLong(0, newDeptCode.longValue()).setLong(1, oldDeptCode.longValue()).executeUpdate();
    }


    public String getGenPrpNO(Long grantCode, Long subGrantCode, Long helpGrantCode, String subjectCode, String statYear) throws SQLException {
        /* 153 */
        String prpNo = "";
        /* 154 */
        SQLQuery sq = getSession().createSQLQuery("select FN_PROPOSAL_GEN_NO(?,?,?,?,?) from dual");
        /* 155 */
        sq.setLong(0, (grantCode == null) ? 0L : grantCode.longValue());
        /* 156 */
        sq.setLong(1, (subGrantCode == null) ? 0L : subGrantCode.longValue());
        /* 157 */
        sq.setLong(2, (helpGrantCode == null) ? 0L : helpGrantCode.longValue());
        /* 158 */
        sq.setString(3, subjectCode);
        /* 159 */
        sq.setString(4, statYear);
        /* 160 */
        prpNo = (String) sq.uniqueResult();

        /* 162 */
        return prpNo;
    }


    public Integer getJSSLFlag(String grantNo, String subGrantNo, String helpGrantNo) throws SQLException {
        /* 212 */
        Integer flagJZSL = Integer.valueOf(0);
        /* 213 */
        SQLQuery sq = getSession().createSQLQuery("select FN_PROPOSAL_IS_JZSL(?,?,?) from dual");
        /* 214 */
        sq.setString(0, (grantNo == null) ? "" : grantNo);
        /* 215 */
        sq.setString(1, (subGrantNo == null) ? "" : subGrantNo);
        /* 216 */
        sq.setString(2, (helpGrantNo == null) ? "" : helpGrantNo);

        /* 218 */
        flagJZSL = Integer.valueOf(sq.uniqueResult().toString());

        /* 220 */
        return flagJZSL;
    }

    public List<Map<String, Object>> getPsnCodes() {
        /* 224 */
        SQLQuery sq = getSession().createSQLQuery("select psn_code form project");
        /* 225 */
        List<Map<String, Object>> list = sq.list();
        /* 226 */
        return list;
    }


    public List<Proposal> findAllPrps() {
        /* 235 */
        StringBuilder hql = new StringBuilder();
        /* 236 */
        hql.append("from Proposal p where p.psnCode=0");
        /* 237 */
        return createQuery(hql.toString(), new Object[0]).list();
    }

    public void updateProposal(Long prpCode, Long psnCode) throws DaoException {
        /* 241 */
        StringBuilder hql = new StringBuilder();
        /* 242 */
        hql.append("update Proposal p set p.psnCode=? where p.prpCode=?");
        /* 243 */
        createQuery(hql.toString(), new Object[]{psnCode, prpCode}).executeUpdate();
    }

    public void updateProposal(Proposal proposal) throws DaoException {
        /* 247 */
        getSession().update(proposal);
    }


    public void callPrpProc(Proposal proposal) throws Exception {
        /* 258 */
        CallableStatement cs = getSession().connection().prepareCall("{call proposal_proc(?,?,?)}");
        /* 259 */
        cs.setLong(1, proposal.getPrpCode().longValue());
        /* 260 */
        cs.setString(2, proposal.getStatus());
        /* 261 */
        cs.setString(3, (proposal.getSubGrantCode() == null) ? "" : proposal.getSubGrantCode().toString());
        /* 262 */
        cs.execute();
        /* 263 */
        cs.close();
    }


    public void markReview(Long prpCode) {
        try {
            /* 269 */
            getSession().createSQLQuery("call ATVCONTAINER_INIT(2, " + prpCode + ")");
            /* 270 */
        } catch (Exception e) {
            /* 271 */
            this.logger.error("-------call ATVCONTAINER_INIT error" + prpCode, e);
        }
    }


    public boolean whetherCanApprove(List<Long> prpCodes) {
        /* 282 */
        String hql = "select t from Proposal t where t.prpCode in ( :prpCodes ) and t.status != :status";
        /* 283 */
        Query query = createQuery(hql, new Object[0]);
        /* 284 */
        query.setParameterList("prpCodes", prpCodes);
        /* 285 */
        query.setParameter("status", "03");
        /* 286 */
        return !(query.list().size() > 0);
    }


    public boolean whetherCanApprove(List<Long> prpCodes, String status) {
        /* 296 */
        String[] strStatus = status.split(",");
        /* 297 */
        List<String> statusList = new ArrayList<String>();
        /* 298 */
        for (String ss : strStatus) {
            /* 299 */
            if (NumberUtils.isDigits(ss)) {
                /* 300 */
                statusList.add(ss);
            }
        }
        /* 303 */
        String hql = "select t from Proposal t where t.prpCode in ( :prpCodes ) and t.status not in (:status)";
        /* 304 */
        Query query = createQuery(hql, new Object[0]);
        /* 305 */
        query.setParameterList("prpCodes", prpCodes);
        /* 306 */
        query.setParameterList("status", statusList);
        /* 307 */
        return !(query.list().size() > 0);
    }


    public List<Map<String, Object>> searchAuditCommentGrantInfo(Map<String, Object> params) {
        /* 317 */
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        /* 318 */
        StringBuilder sb = new StringBuilder();
        /* 319 */
        sb.append(" select p.grant_code, g.zh_cn_grant_name as grant_name, count(0) total");
        /* 320 */
        sb.append(" from proposal p");
        /* 321 */
        sb.append(" left join grant_setting g on p.grant_code = g.grant_code");
        /* 322 */
        sb.append(" where 1 = 1");
        /* 323 */
        sb.append(" and p.status is not null and p.status not in ('00','01','10','98','99') ");
        /* 324 */
        if (params.containsKey("orgCode")) {
            /* 325 */
            sb.append(" and p.org_code = :orgCode");
        }
        /* 327 */
        sb.append(" group by p.grant_code, g.zh_cn_grant_name");
        /* 328 */
        list = getSession().createSQLQuery(sb.toString()).setProperties(params).setResultTransformer((ResultTransformer) Transformers.ALIAS_TO_ENTITY_MAP).list();

        /* 330 */
        return list;
    }


    public List<ProposalCached> findProposalByPsnCode(Long psnCode) {
        /* 342 */
        Query query = createQuery("from ProposalCached where psnCode = :psnCode", new Object[0]);
        /* 343 */
        query.setLong("psnCode", psnCode.longValue());
        /* 344 */
        return query.list();
    }


    public List<ProposalCached> findProposal4OrgByPsnCode(Long orgCode, Long psnCode) {
        /* 357 */
        Query query = createQuery("from ProposalCached where orgCode = :orgCode and psnCode = :psnCode", new Object[0]);
        /* 358 */
        query.setLong("orgCode", orgCode.longValue());
        /* 359 */
        query.setLong("psnCode", psnCode.longValue());
        /* 360 */
        return query.list();
    }


    public List<Map<String, Object>> findProposal(Long orgCode, Long psnCode, Long grantCode) throws DaoException {
        /* 376 */
        List<Object> paramsList = new ArrayList();
        /* 377 */
        paramsList.add(orgCode);
        /* 378 */
        paramsList.add(psnCode);
        /* 379 */
        paramsList.add(grantCode);
        /* 380 */
        Query query = createSqlQuery("select t.zh_title as title, c.zh_cn_caption as statusName from Proposal_Cached t left join const_dictionary c on t.status = c.code and c.category = 'proposal_status' where org_code=? and psn_code=? and grant_code=?", paramsList).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);


        /* 383 */
        query.setReadOnly(true);
        /* 384 */
        return query.list();
    }


    public Map<String, Object> findProposal(Long prpCode) {
        /* 389 */
        Query query = getSession().createSQLQuery("select p.zh_name as fzr,p.open_id as openId,t.zh_title as zhTitle,t.psn_code as psnCode, c.zh_cn_caption as statusName  from proposal t left join person p on p.psn_code=t.psn_code left join const_dictionary c   on t.status = c.code  and c.category = 'proposal_status'  where t.prp_code=?").addScalar("fzr", (Type) StandardBasicTypes.STRING).addScalar("openId", (Type) StandardBasicTypes.STRING).addScalar("zhTitle", (Type) StandardBasicTypes.STRING).addScalar("psnCode", (Type) StandardBasicTypes.STRING).addScalar("statusName", (Type) StandardBasicTypes.STRING).setLong(0, prpCode.longValue()).setResultTransformer((ResultTransformer) Transformers.ALIAS_TO_ENTITY_MAP);

        /* 391 */
        query.setReadOnly(true);
        /* 392 */
        return (Map<String, Object>) query.uniqueResult();
    }


    public List<Map<String, Object>> findAuthoredExistProposal(Long orgCode, Long psnCode, Long grantCode, String status) {
        /* 409 */
        List<Object> paramsList = new ArrayList();
        /* 410 */
        paramsList.add(orgCode);
        /* 411 */
        paramsList.add(psnCode);
        /* 412 */
        paramsList.add(grantCode);
        /* 413 */
        paramsList.add(status);
        /* 414 */
        Query query = createSqlQuery("select t.zh_title as title, c.zh_cn_caption as statusName from Proposal_Cached t left join const_dictionary c on t.status = c.code and c.category = 'proposal_status' where org_code=? and psn_code=? and sub_grant_code=? and status <> ?", paramsList).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);


        /* 417 */
        query.setReadOnly(true);
        /* 418 */
        return query.list();
    }


    public void updatePrpNo(Long prpCode) {
        /* 491 */
        String sql = "update proposal p set p.prp_no=fn_proposal_gen_no_shanxi(?) where prp_code=?";




        /* 496 */
        update(sql, new Object[]{prpCode, prpCode});
    }


    public void updateCgjdPrp(String opCode, Map<String, Object> params) {
        try {
            /* 503 */
            CallableStatement cs = getSession().connection().prepareCall("{call sp_qhhCgjd_updPrp(?,?,?,?)}");
            /* 504 */
            cs.setLong(1, Long.parseLong(opCode));
            /* 505 */
            cs.setString(2, params.get("cgmc").toString());
            /* 506 */
            cs.setString(3, params.get("reviewResult").toString());
            /* 507 */
            cs.setString(4, params.get("jiandingDate").toString());
            /* 508 */
            cs.executeUpdate();
            /* 509 */
            cs.close();
            /* 510 */
        } catch (SQLException e) {
            /* 511 */
            e.printStackTrace();
        }
    }


    public void updatePrpAcceptDate(String prpCodes) {
        try {
            /* 518 */
            CallableStatement cs = getSession().connection().prepareCall("{call sp_qhhCgjd_setAcceptDate(?)}");
            /* 519 */
            cs.setLong(1, Long.parseLong(prpCodes));
            /* 520 */
            cs.executeUpdate();
            /* 521 */
            cs.close();
            /* 522 */
        } catch (SQLException e) {
            /* 523 */
            e.printStackTrace();
        }
    }


    public String selectIsAllowCheckIn(String newOrgCode) {
        /* 529 */
        Long org_code = Long.valueOf(Long.parseLong(newOrgCode));
        /* 530 */
        String sql = "select nvl(extractvalue(o.org_xml,'/data/organization/is_technology_org_value'),2) as result from organization_extend o where o.org_code = ? and o.org_verson = (select max(b.org_verson) from organization_extend b where b.org_code = ?)";
        /* 531 */
        String flag = (String) getSession().createSQLQuery(sql).setParameter(0, org_code).setParameter(1, org_code).uniqueResult();


        /* 534 */
        return flag;
    }


    public boolean whetherCanLoadExcel(String grantCode) {
        /* 539 */
        String sql = "select gs1.grant_code from grant_setting gs1 where gs1.grant_level = 1 start with gs1.grant_code = ? connect by prior gs1.parent_code = gs1.grant_code";
        /* 540 */
        List<Object> paramList = new ArrayList();
        /* 541 */
        paramList.add(grantCode);
        /* 542 */
        SQLQuery sQLQuery = createSqlQuery(sql, paramList);
        /* 543 */
        sQLQuery.setParameter(0, grantCode);
        /* 544 */
        boolean canLoadExcel = false;
        /* 545 */
        if ("2".equals(sQLQuery.list().get(0).toString()) || "4".equals(sQLQuery.list().get(0).toString())) {
            /* 546 */
            canLoadExcel = true;
        }
        /* 548 */
        return canLoadExcel;
    }


    public Proposal viewProposal(Long prpCode) {
///* 558 */     if (SecurityUtils.isAuthority("A_PROPOSAL_VIEW_PSN")) {
///* 559 */       String str = "from Proposal where psnCode=? and prpCode=?";
///* 560 */       return (Proposal)findUnique(str, new Object[] { SecurityUtils.getCurrentUserId(), prpCode });
///* 561 */     }  if (SecurityUtils.isAuthority("A_PROPOSAL_VIEW_PRJPSN")) {
///* 562 */       String str = "from Proposal where (psnCode=? or prjPsnCode=?) and prpCode=?";
///* 563 */       return (Proposal)findUnique(str, new Object[] { SecurityUtils.getCurrentUserId(), SecurityUtils.getCurrentUserId(), prpCode });
//    }
///* 565 */     if (SecurityUtils.isAuthority("A_PROPOSAL_VIEW_ORG")) {
///* 566 */       String str = "from Proposal where (orgCode=? or dept_code=? ) and prpCode=?";
///* 567 */       return (Proposal)findUnique(str, new Object[] { (SecurityUtils.getAdminOrgCode().longValue() == -1L) ? SecurityUtils.getCurrentOrgCode() : SecurityUtils.getAdminOrgCode(), SecurityUtils.getCurrentDeptCode(), prpCode });
//    }

        /* 570 */
        String hql = "from Proposal where prpCode=?";
        /* 571 */
        return (Proposal) findUnique(hql, new Object[]{prpCode});
    }


}


/* Location:              D:\文件kjt\WEB-INF\lib\egrant-code-2.2.20240205.1659.jar!\com\iris\egrant\dao\proposal\ProposalDao.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       1.1.3
 */